package com.lexing;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.lexing.bean.BaseCondition;
import com.lexing.bean.Enviroment;
import com.lexing.bean.Product;
import com.lexing.bean.ProductType;

public class ExcelHandler {
	public static List<ProductType> readProductType(Sheet sheet) {
		int rowStart = Math.max(1, sheet.getFirstRowNum());
	    int rowEnd = Math.min(1400, sheet.getLastRowNum());
	    ArrayList<ProductType> list = new ArrayList<ProductType>();
	    for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
			Row row = sheet.getRow(rowNum);
			if(row == null) continue;
			ProductType productType = new ProductType();
			productType.setId((int)(row.getCell(0).getNumericCellValue()));
			productType.setName(getRowString(row,1));
			productType.setImagePath(getRowString(row,2));
			list.add(productType);
		}
	    return list;
	}
	
	public static List<Enviroment> readEnviroment(Sheet sheet) {
		ArrayList<Enviroment> list = new ArrayList<Enviroment>();
		int rowStart = Math.max(1, sheet.getFirstRowNum());
	    int rowEnd = Math.min(1400, sheet.getLastRowNum());
	    for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
	    	Row row = sheet.getRow(rowNum);
			if(row == null) continue;
			Enviroment enviroment = new Enviroment();
			enviroment.setId((int)row.getCell(0).getNumericCellValue());
			enviroment.setName(getRowString(row,1));
			enviroment.setIcon(getRowString(row,2));
			enviroment.setDescription(getRowString(row,3));
			enviroment.setImagePath(getRowString(row,4));
			list.add(enviroment);
	    }
		return list;
	}
//	private static<T> List<T> readTemplate(Sheet sheet,int rowStart,int rowEnd) {
//	    ArrayList<T> list = new ArrayList<T>();
//	    for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
//	    	Row row = sheet.getRow(rowNum);
//			if(row == null) continue;
//			
//	    }
//	    return list;
//	}
	public static List<BaseCondition> readBaseConditions(Sheet sheet) {
		int rowStart = Math.max(1, sheet.getFirstRowNum());
	    int rowEnd = Math.min(1400, sheet.getLastRowNum());
		ArrayList<BaseCondition> list = new ArrayList<BaseCondition>();
		for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
			Row row = sheet.getRow(rowNum);
			if(row == null) continue;
			BaseCondition condition = new BaseCondition();
			condition.setId((int)row.getCell(0).getNumericCellValue());
			condition.setDescription(getRowString(row,1));
			condition.setImagePath(getRowString(row,2));
			list.add(condition);
		}
		return list;
	}
	
	public static List<Product> readProducts(Sheet sheet,int start,int end) {
		int rowStart = Math.max(start, sheet.getFirstRowNum());
	    int rowEnd = Math.min(end, sheet.getLastRowNum());
	    ArrayList<Product> list = new ArrayList<Product>();
		for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
			Row row = sheet.getRow(rowNum);
			if(row == null) continue;
			Product product = new Product();
			product.setProductId(rowNum);
			product.setProductType((int)row.getCell(0).getNumericCellValue());
			product.setName(getRowString(row,1));
			product.setImagePath(getRowString(row,2));
			product.setShortDesc(getRowString(row,3));
			product.setDescription1(getRowString(row,4));
			product.setDescription2(getRowString(row,5));
			product.setFilter(getRowString(row,6));
			list.add(product);
		}
		return list;
	}
	public static List<Product> readProducts(Sheet sheet) {
		return readProducts(sheet, 1, 1400);
	}
	/**
	 * 获取单元格内容，没有用空字串
	 * @param row
	 * @param index
	 * @return
	 */
	private static String getRowString(Row row,int index){
		Cell cell = row.getCell(index);
		if(cell==null) return "";
		return cell.getStringCellValue();
	}
	
}
